{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8c62d7b5",
   "metadata": {},
   "source": [
    "------\n",
    "* 记录人：莫慧敏\n",
    "* week14\n",
    "* 主要参考文档：\n",
    "> 1. [Python官方文档](https://python.org/doc)\n",
    "> 2. [博客-函数](https://www.cnblogs.com/xuzhichao/p/11570285.html)\n",
    "------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "78d2fcd1",
   "metadata": {},
   "source": [
    "## 本周学习目标\n",
    "> 1. 函数的定义\n",
    "> 2. 函数的使用\n",
    "> 3. 函数的参数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "64c35eea",
   "metadata": {},
   "source": [
    "### 函数的定义\n",
    "* 定义一个函数的一般步骤"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2b049cd4",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-29T07:27:42.725394Z",
     "start_time": "2021-11-29T07:27:42.716393Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0112358\n"
     ]
    }
   ],
   "source": [
    "a, b = 0, 1\n",
    "while a < 10:\n",
    "    print(a, end = '')# 0 1 1 2 ...\n",
    "    a, b = b , a+b # a = 1  a = 1 b = 2 a = 2 b = 3\n",
    "print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "66f03829",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-29T07:28:20.747568Z",
     "start_time": "2021-11-29T07:28:20.741568Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0112358132134\n"
     ]
    }
   ],
   "source": [
    "a, b = 0, 1\n",
    "while a < 50:\n",
    "    print(a, end = '')# 0 1 1 2 ...\n",
    "    a, b = b , a+b # a = 1  a = 1 b = 2 a = 2 b = 3\n",
    "print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "dd805a94",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-29T07:28:40.922722Z",
     "start_time": "2021-11-29T07:28:40.902721Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "01123581321345589\n"
     ]
    }
   ],
   "source": [
    "a, b = 0, 1\n",
    "while a < 100:\n",
    "    print(a, end = '')# 0 1 1 2 ...\n",
    "    a, b = b , a+b # a = 1  a = 1 b = 2 a = 2 b = 3\n",
    "print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "86a9a233",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ......我想重复使用某一功能（避免重复造轮子）\n",
    "# ......使用的人可以不懂代码，只要会调用函数即可 function_name()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "59d3d712",
   "metadata": {},
   "source": [
    "> 1.定义函数的一般方法：\n",
    "```\n",
    "def function_name(): # 1. function_name 遵循变量定义的规则 区别数学函数==> f(x) python的函数可以自定义名称\n",
    "\"\"\"函数的注释\"\"\"    # 2.函数的注释可以和使用者交流\n",
    "函数体          # 3. 函数体要和定义函数保持一定的tab缩进\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "09203fd5",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-29T07:46:49.357977Z",
     "start_time": "2021-11-29T07:46:49.349977Z"
    }
   },
   "outputs": [],
   "source": [
    "def fib(n):    # write Fibonacci series up to n\n",
    "#      \"\"\"Print a Fibonacci series up to n.\"\"\"\n",
    "    a, b = 0, 1\n",
    "    while a < n:\n",
    "        print(a,end = '')# 0 1 1 2 ...\n",
    "        a, b = b, a+b #a=1 a=1 b=2 a=2 b=3\n",
    "    print()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4896ad01",
   "metadata": {},
   "source": [
    "### 函数的使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "85d7bdea",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-29T07:47:03.287774Z",
     "start_time": "2021-11-29T07:47:03.273773Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0112358\n"
     ]
    }
   ],
   "source": [
    "fib(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "c4781c60",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-29T07:47:15.512473Z",
     "start_time": "2021-11-29T07:47:15.493472Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0112358132134\n"
     ]
    }
   ],
   "source": [
    "fib(50)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "31d326bc",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-29T07:47:25.220029Z",
     "start_time": "2021-11-29T07:47:25.207028Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "01123581321345589\n"
     ]
    }
   ],
   "source": [
    "fib(100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ddcfed8c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. 请大家写一个函数 实现hello world\n",
    "# 2.请大家写一个函数 实现 hello username"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "561741e7",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-29T07:10:14.407636Z",
     "start_time": "2021-11-29T07:10:14.400636Z"
    }
   },
   "outputs": [],
   "source": [
    "def hello():\n",
    "    \"\"\"打印你好，世界\"\"\"\n",
    "    print(\"hello world!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "088216a2",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-29T07:10:23.562160Z",
     "start_time": "2021-11-29T07:10:23.548159Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world!\n"
     ]
    }
   ],
   "source": [
    "hello()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "6b4905d5",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-29T07:49:04.331697Z",
     "start_time": "2021-11-29T07:49:04.315697Z"
    }
   },
   "outputs": [],
   "source": [
    "def hello_user(username):\n",
    "    \"\"\"打印，你好xxx同学\"\"\"\n",
    "    print(\"你好，\" +username.title()+\"同学\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "bc79a3fe",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-29T07:49:26.589971Z",
     "start_time": "2021-11-29T07:49:26.572970Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你好，Jack同学\n"
     ]
    }
   ],
   "source": [
    "hello_user(\"jack\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b61d23b9",
   "metadata": {},
   "source": [
    "* 思考：函数是否有 参数？\n",
    "* 注意：\n",
    "> 1. 在第一行定义函数中，不需要任何信息就能完成工作，因此括号是空的（即便如此，括号必不可少！）\n",
    "> 2. 要调用函数，可以依次指定函数名以及括号括起的必要信息。在第五行代码中，因为此函数hello()括号不需要任何信息，只需要输入hello()即可。和预期效果一样，打印hello，world！"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5616f904",
   "metadata": {},
   "source": [
    "### 函数的参数\n",
    "> 1. 形式参数\n",
    ">> 1. 定义函数时，写在function_name括号里面的参数\n",
    ">> 2. 在定义函数时，形式参数不占空间\n",
    "> 2. 实际参数\n",
    ">> 1. 调用函数时，写在function_name括号里面的参数\n",
    ">> 2. 在使用函数时，形式参数被分配空间，变为实际参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "3783047b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-29T08:35:02.597461Z",
     "start_time": "2021-11-29T08:35:02.589461Z"
    }
   },
   "outputs": [],
   "source": [
    "def describe_pet(animal_type,pet_name): # animal_type,pet_name是形式参数 形参\n",
    "    \"\"\"显示宠物信息\"\"\"\n",
    "    print(\"\\nI have a \" +animal_type+\".\")\n",
    "    print(\"My \"+animal_type+\"'s name is\"+pet_name.title() + \".\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "95ef05c7",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-29T08:35:06.498684Z",
     "start_time": "2021-11-29T08:35:06.488684Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have a dog.\n",
      "My dog's name is豆豆.\n"
     ]
    }
   ],
   "source": [
    "describe_pet(\"dog\",\"豆豆\") # \"dag\",\"豆豆\" 实际参数 实参"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "a055326d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-29T08:35:59.507716Z",
     "start_time": "2021-11-29T08:35:59.496716Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have a 豆豆.\n",
      "My 豆豆's name isDog.\n"
     ]
    }
   ],
   "source": [
    "describe_pet(\"豆豆\",\"dog\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0bc53286",
   "metadata": {},
   "source": [
    "* 当函数具有多个参数时\n",
    "> 1. 位置参数\n",
    ">> "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "25f2380b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ff87c802",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bc31bdd5",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
